$(document).ready(function() {
    $('#listFormSubmit').click(function() {
        var taskName = $('#taskName').val();
        var taskPlug = $('#taskPlug').val();
        var taskSleepTime = $('#taskSleepTime').val();
        var taskNum = $('#taskNum').val();
        var taskClientIp = $('#taskClientIp').val();
        var needCookie = $('#needCookie').is(':checked') ? 1 : 0;
        var needAgent = $('#needAgent').is(':checked') ? 1 : 0;
        var needHeader = $('#needHeader').is(':checked') ? 1 : 0;

        $.post(
            'taskSave.html',
            {taskName:taskName, taskPlug:taskPlug, taskSleepTime:taskSleepTime, taskNum:taskNum, taskClientIp:taskClientIp,needCookie:needCookie, needAgent:needAgent, needHeader:needHeader},
            function(result) {
                console.log(result)
            }
        )
    })

    $('.updateListFormSubmit').click(function() {
        var dataId = $(this).attr('dataCountId');
        var updateTaskId = $('#updateTaskId' + dataId).val();
        var updateTaskSleepTime = $('#updateTaskSleepTime' + dataId).val();
        var updateTaskNum = $('#updateTaskNum' + dataId).val();

        $.post(
            'taskUpdate.html',
            {updateTaskId:updateTaskId, updateTaskSleepTime:updateTaskSleepTime, updateTaskNum:updateTaskNum},
            function(result) {
                console.log(result)
            }
        )
    })

    $("#taskManageStart").on('show.bs.modal', function(event) {
        var button = $(event.relatedTarget)
        var taskId = button.data('taskid')
        var taskName = button.data('taskname')
        var modalBody = $(this).find('.modal-body')

        var innerHtml = taskName + " (" + taskId + ") 开始运行...<br/>"
        modalBody.html(innerHtml)

        $.post(
            'taskRun.html',
            {taskId:taskId},
            function(result){
                modalBody.append('添加任务成功，等待客户端响应...<br/>')
                var invar = self.setInterval(function(){checkHeartBeat(taskId, invar, modalBody)}, 1000)
            }
        )
    })

    $("#taskManageStop").on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget)
        var taskId = button.data('taskid')
        var taskName = button.data('taskname')
        var modalBody = $(this).find('.modal-body')

        var innerHtml = taskName + " (" + taskId + ") 准备停止...<br/>"
        modalBody.html(innerHtml)

        $.post(
            'taskStop.html',
            {taskId:taskId},
            function () {
                setTimeout(function(){
                    modalBody.append('停止任务成功!<br/>')
                    location.reload()
                }, 1000)
            }
        )
    })

    $('.updateListIpFormSubmit').click(function() {
        var dataId = $(this).attr('dataCountId');
        var updateIpTaskId = $('#updateIpTaskId' + dataId).val();
        var updateIpTaskNowIp = $('#updateIpTaskNowIp' + dataId).val();

        $.post(
            'taskIpUpdate.html',
            {updateIpTaskId:updateIpTaskId, updateIpTaskNowIp:updateIpTaskNowIp},
            function(result) {
                console.log(result)
            }
        )
    })
})

function checkHeartBeat(taskId, invar, modalBody)
{
    $.post(
        'taskCheckHeartBeat.html',
        {taskId:taskId},
        function(result) {
            if (result == 'true') {
                window.clearInterval(invar)
                modalBody.append("运行成功!")
                location.reload()
            } else {
                console.log("fuckyoubitch")
            }
        }
    )
}